package com.lemon.tuvi2015;

import org.json.JSONObject;

import com.lemon.tuvi2015.BoiCotheFragment.AsyncTaskParseJson;
import com.lemon.tuvi2015.util.BaseEngine;
import com.lemon.tuvi2015.util.JsonParser;
import com.lemon.tuvi2015.util.TagName;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;

public class BoiKhuonmatFragment extends Fragment {
	EditText editText;
	ImageView imageView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View android = inflater.inflate(R.layout.boinotruoi_khuonmat, container, false);
		findviewByID(android);
		imageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new AsyncTaskParseJson().execute("");
			}
		});

		return android;
	}

	void findviewByID(View view) {
		editText = (EditText) view.findViewById(R.id.editText);
		imageView = (ImageView) view.findViewById(R.id.imageview);
	}

	public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
		JSONObject jsonObject;
		ProgressDialog progressDialog = new ProgressDialog(getActivity());

		@Override
		protected void onPreExecute() {
			if (!progressDialog.isShowing())
				progressDialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			String str = JsonParser.boinotruoi("1", editText.getText().toString());
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			BaseEngine.showLogformLongString("", result);
			if (progressDialog.isShowing())
				progressDialog.hide();
			if (result != null)
				try {
					Intent intent = new Intent(getActivity(), DetailsActivity.class);
					intent.putExtra(TagName.TITLE, "Nốt ruồi: " + editText.getText().toString());
					intent.putExtra(TagName.KEY, result);
					startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}
}
